package com.example.myapplication;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;


import java.util.ArrayList;

public class MainDialogActivity extends AppCompatActivity implements View.OnClickListener {
    private int yourChoice;
    private ArrayList<Integer> yourChoices = new ArrayList<>();
    private Button btDialogOne;
    private Button btDialogTwo;
    private Button btDialogThree;
    private Button btDialogFour;
    private Button btDialogSix;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_dialog);
        System.out.println("Dialog");

        //获取控件
        btDialogOne = findViewById(R.id.btn_dialog_one);
        btDialogTwo = findViewById(R.id.btn_dialog_two);
        btDialogThree = findViewById(R.id.btn_dialog_three);
        btDialogFour = findViewById(R.id.btn_dialog_four);
        btDialogSix = findViewById(R.id.btn_dialog_six);

        //监听事件
        btDialogOne.setOnClickListener(this);
        btDialogTwo.setOnClickListener(this);
        btDialogThree.setOnClickListener(this);
        btDialogFour.setOnClickListener(this);
        btDialogSix.setOnClickListener(this);
    }

    //提示框方法
    private void showToastShort(String str) {
        //提示框居中显示
//        Toast toast = Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT);
//        toast.setGravity(Gravity.CENTER, 0, 0);
//        toast.show();
        //提示框默认显示
        Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT).show();
    }

    /**
     * 普通dialog
     *
     * @setIcon 设置对话框图标
     * @setTitle 设置对话框标题
     * @setMessage 设置对话框消息提示
     * setXXX方法返回Dialog对象，因此可以链式设置属性
     */
    public void DialogOne() {
        AlertDialog.Builder normalDialog = new AlertDialog.Builder(MainDialogActivity.this);
        normalDialog.setIcon(R.mipmap.ic_launcher);
        normalDialog.setTitle("你的娱乐方式是玩游戏吗？");
        normalDialog.setMessage("选择哪一个?");
        normalDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        showToastShort("你点击了确定");
                    }
                });
        normalDialog.setNegativeButton("关闭",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        showToastShort("你点击了关闭");
                    }
                });
        normalDialog.show();// 显示
    }

    /**
     * 列表Dialog
     */
    public void DialogTwo() {
        final String[] items = {"西山居", "米哈游", "bilibili游戏", "鹰角网络"};
        AlertDialog.Builder listDialog =
                new AlertDialog.Builder(MainDialogActivity.this);
        listDialog.setTitle("这里有四个选择哪一个");
        listDialog.setItems(items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                showToastShort("你点击了" + items[which]);
            }
        });
        listDialog.show();
    }

    /**
     * 单选Dialog
     */
    public void DialogThree() {
        final String[] items = {"西山居", "米哈游", "bilibili游戏", "鹰角网络"};
        yourChoice = -1;
        AlertDialog.Builder singleChoiceDialog =
                new AlertDialog.Builder(MainDialogActivity.this);
        singleChoiceDialog.setTitle("这里有四个选择哪一个（只能选择一个）");
        // 第二个参数是默认选项，此处设置为0
        singleChoiceDialog.setSingleChoiceItems(items, 0,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        yourChoice = which;
                    }
                });
        singleChoiceDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (yourChoice != -1) {
                            showToastShort("你选择了" + items[yourChoice]);
                        }
                    }
                });
        singleChoiceDialog.show();
    }

    /**
     * 多选Dialog
     */
    public void DialogFour() {
        final String[] items = {"西山居", "米哈游", "bilibili游戏", "鹰角网络"};
        // 设置默认选中的选项，全为false默认均未选中
        final boolean initChoiceSets[] = {false, false, false, false};
        yourChoices.clear();
        AlertDialog.Builder multiChoiceDialog =
                new AlertDialog.Builder(MainDialogActivity.this);
        multiChoiceDialog.setTitle("这里有四个选择哪一个（多选）");
        multiChoiceDialog.setMultiChoiceItems(items, initChoiceSets,
                new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which,
                                        boolean isChecked) {
                        if (isChecked) {
                            yourChoices.add(which);
                        } else {
                            yourChoices.remove(which);
                        }
                    }
                });
        multiChoiceDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        int size = yourChoices.size();
                        String str = "";
                        for (int i = 0; i < size; i++) {
                            str += items[yourChoices.get(i)] + " ";
                        }
                        showToastShort("你选中了" + str);
                    }
                });
        multiChoiceDialog.show();
    }


    /**
     * 进度条dailog
     *
     * @setProgress 设置初始进度
     * @setProgressStyle 设置样式（水平进度条）
     * @setMax 设置进度最大值
     */
    public void DialogSix() {
        final int MAX_PROGRESS = 100;
        final ProgressDialog progressDialog =
                new ProgressDialog(MainDialogActivity.this);
        progressDialog.setProgress(0);
        progressDialog.setTitle("选择倒计时！！！");
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.setMax(MAX_PROGRESS);
        progressDialog.show();
        /* 模拟进度增加的过程，新开一个线程，每个100ms，进度增加1*/
        new Thread(new Runnable() {
            @Override
            public void run() {
                int progress = 0;
                while (progress < MAX_PROGRESS) {
                    try {
                        Thread.sleep(100);
                        progress++;
                        progressDialog.setProgress(progress);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                // 进度达到最大值后，窗口消失
                progressDialog.cancel();
            }
        }).start();
    }


    /**
     * 点击事件
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_dialog_one:
                DialogOne();//显示dialog
                break;
            case R.id.btn_dialog_two:
                DialogTwo();
                break;
            case R.id.btn_dialog_three:
                DialogThree();
                break;
            case R.id.btn_dialog_four:
                DialogFour();
                break;
            case R.id.btn_dialog_six:
                DialogSix();
                break;
        }
    }
}
